package com.jixiantubu.wode;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import com.example.jixiantubu.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jixiantubu.adapter.Wo_YoujiAdapter;
import com.jixiantubu.bean.Collectbean;
import com.jixiantubu.bean.Travelsbean;
import com.jixiantubu.jinnang.JinNang_YouJi_JuTiNeiRong;
import com.jixiantubu.util.MyApplication;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ImageView;
import android.widget.ListView;

public class Wo_youjiActivity extends Activity {
	ListView wo_youji_lv;
	//
	List<Travelsbean> list = new ArrayList<Travelsbean>();
	HttpUtils httpUtils = new HttpUtils();
	Wo_YoujiAdapter adapter;
	MyApplication application;
	ImageView shezhifanhui;
	Collectbean collectbean = new Collectbean();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Window window = this.getWindow();
		window.requestFeature(window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_wo_youji);
		initViews();
		initData();

	}

	private void initData() {
		adapter = new Wo_YoujiAdapter(Wo_youjiActivity.this, list);
		wo_youji_lv.setAdapter(adapter);
		httpUtils = new HttpUtils();

		String url = "http://" + MyApplication.getIP()
				+ ":8080/Jinnang/WoGuanzhu";
		RequestParams requParams = new RequestParams();

		requParams.addBodyParameter("key", "9");
		requParams.addBodyParameter("user_id", MyApplication.user_id);
		Log.e("收藏的key和user_id", MyApplication.user_id);

		httpUtils.send(HttpMethod.POST, url, requParams,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						// 获取服务器端返回的数据结果
						String result = arg0.result;
						// shiyongGson框架进行json解析
						Gson gson = new Gson();
						Type typeOfT = new TypeToken<List<Travelsbean>>() {
						}.getType();
						List<Travelsbean> list1 = gson
								.fromJson(result, typeOfT);
						list.addAll(list1);
						Log.e("hua-eee-----", list.toString());
						adapter.notifyDataSetChanged();
					}

				});

	}

	private void initViews() {
		shezhifanhui = (ImageView) findViewById(R.id.wo_shezhi);
		wo_youji_lv = (ListView) findViewById(R.id.wodeyouji_lv);

		// 单击跳转到相应的界面
		wo_youji_lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				Intent intent = new Intent(Wo_youjiActivity.this,
						JinNang_YouJi_JuTiNeiRong.class);
				intent.putExtra("neirong", list.get(position));
				startActivity(intent);
			}
		});
		// 添加长按点击,得到点中的index，即参数arg2
		wo_youji_lv.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
				dialog(position);
				com.lidroid.xutils.http.RequestParams requParams = new com.lidroid.xutils.http.RequestParams();
				requParams.addBodyParameter("key", "10");
				String p = list.get(position).getT_id() + "";
				System.out.println("----------------" + p);
				requParams.addBodyParameter("t_id", p.trim());

				requParams.addBodyParameter("user_id", MyApplication.user_id);
				String url = "http://" + MyApplication.getIP()
						+ ":8080/Jinnang/WoGuanzhu";
				httpUtils.send(HttpMethod.POST, url, requParams,
						new RequestCallBack<String>() {

							@Override
							public void onFailure(HttpException arg0,
									String arg1) {
							}

							@Override
							public void onSuccess(ResponseInfo<String> arg0) {
								// Intent intent=new
								// Intent(context,KepushoucangActivity.class);
								// context.startActivity(intent);
								adapter.notifyDataSetChanged();
							}
						});
				return false;
			}
		});

	}

	private void dialog(final int position) {
		// TODO Auto-generated method stub
		AlertDialog.Builder builder = new Builder(Wo_youjiActivity.this);
		builder.setMessage("确认删除吗？");
		builder.setTitle("提示");
		builder.setPositiveButton("确认", new OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				list.remove(position);
				adapter.notifyDataSetChanged();
			}

		});
		builder.setNegativeButton("取消", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();

	}

	public void fanhui(View v) {
		finish();
	}

}
